package com.zcx.qshop.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.zcx.helper.activity.AppFragment;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.qshop.QSApplication;
import com.zcx.qshop.R;
import com.zcx.qshop.activity.TwoListActivity;
import com.zcx.qshop.adapter.ClassifyAdapter;
import com.zcx.qshop.conn.JsonAreaAsyGet;
import com.zcx.qshop.conn.JsonTypelistAsyGet;
import com.zcx.qshop.view.TitleView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/8/8 0008.
 */
public class ClassifyFragment extends AppFragment{

    public static OnCityChangeCallBack onCityChangeCallBack;

    @BoundView(R.id.title_view) private TitleView titleView;

    @BoundView(R.id.classify_list_view) private ListView listView;

    private List<JsonTypelistAsyGet.TypeOne> list = new ArrayList<>();

    private ClassifyAdapter adapter;

    private JsonTypelistAsyGet jsonTypelistAsyGet = new JsonTypelistAsyGet("", "", new AsyCallBack<JsonTypelistAsyGet.Info>(){

        @Override
        public void onSuccess(String toast, int type, JsonTypelistAsyGet.Info info) throws Exception {

            list.clear();

            list.addAll(info.typeOnes);

            adapter.notifyDataSetChanged();

        }

    });

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = BoundViewHelper.boundView(this, QSApplication.ScaleScreenHelper.loadView((ViewGroup) inflater.inflate(R.layout.fragment_classify, null)));

        titleView.showStyle(TitleView.Style.FULL);

        titleView.showType(TitleView.Type.NAME_ONE);

        titleView.setTitleName("分类");

        listView.setAdapter(adapter = new ClassifyAdapter(getActivity(), list));

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                startActivity(new Intent(getActivity(), TwoListActivity.class).putExtra("TypeOne", adapter.getItem(position)));

            }

        });

        onCityChangeCallBack = new OnCityChangeCallBack() {

            @Override
            public void onCityChange(JsonAreaAsyGet.Area area) {

                jsonTypelistAsyGet.areaid = area.code;

                jsonTypelistAsyGet.execute(getActivity());

            }

        };

        onCityChangeCallBack.onCityChange(QSApplication.QSPreferences.readCity());

        return view;

    }

    public interface OnCityChangeCallBack{

        void onCityChange(JsonAreaAsyGet.Area area);

    }

}
